﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using HtmlAgilityPack;
using NewsCrawler.Common.Models;
using CrawlerManager;
using NewsCrawler.Common.Interfaces;

namespace Chip
{
    public class SiteCrawler : IParser
    {
        public void Parse(Article article)
        {
            WebClient client = new WebClient();
            string filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
            client.DownloadFile(article.Url, filePath);

            HtmlDocument doc = new HtmlDocument();
            doc.Load(filePath);

            //HtmlNode altcsol = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/div[5]/div[1]");
            //HtmlNode tarih = altcsol.SelectSingleNode("//div[20]/table[1]/tr[2]/td[1]/span[1]");
            HtmlNode tarihNode = doc.DocumentNode.SelectSingleNode("//div[@class='tarih']");
            HtmlNode articleNode = doc.DocumentNode.SelectSingleNode("//div[@id='contextual']");
            HtmlNode nofollowNode = articleNode.SelectSingleNode("a[@rel='nofollow']");
            HtmlNode imageNode =
                doc.DocumentNode.SelectSingleNode("//div[@class='content_image_image']").SelectSingleNode("img");

            article.Folder = "Chip";
            article.Images.Add(GetImagePath(imageNode, client, article.Folder));
            if (nofollowNode != null)
            {
                article.Content = articleNode.InnerHtml.Replace(nofollowNode.OuterHtml, string.Empty);
            }
            article.PublishDate = ParseTarih(tarihNode.InnerHtml);
            article.CultureCode = "tr-TR";
        }

        public string ImagePath(string folder)
        {
            var path = Path.Combine(Environment.CurrentDirectory, "images", folder);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path;
        }

        private string GetImagePath(HtmlNode image, WebClient client, string folder)
        {
            string value = image.Attributes["src"].Value;
            string fileName = Path.Combine(ImagePath(folder), Path.GetFileName(value));
            client.DownloadFile("http://www.chip.com.tr" + value, fileName);
            return fileName;
        }

        private DateTime ParseTarih(string innerHtml)
        {
            DateTime date = new DateTime();
            string[] parsed = innerHtml.Replace(" ", "").Split(',');
            if (parsed[0] == "Bugün")
            {
                date = CrawlerHelper.CopyDateTime(date, DateTime.Today, new int[] { }, true);
                DateTime saat = DateTime.Parse(parsed[1]);
                date = CrawlerHelper.CopyDateTime(saat, date, new int[] { 2, 3 }, false);
            }
            else
            {
                date = DateTime.Parse(innerHtml);
            }


            return date;
        }
    }
}
